package com.atguigu.gmall.wms.lister;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.wms.mapper.WmsWareSkuMapper;
import com.atguigu.gmall.wms.vo.SkuLockVo;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.yaml.snakeyaml.events.Event;

import java.io.IOException;
import java.util.List;

@Component
public class WareLister {

    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    WmsWareSkuMapper wareSkuMapper;

    private final static String STOCK_WARE = "stock_ware";

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "STOCK_UNLOCK_QUEUE"),
            exchange = @Exchange(value = "ORDER_EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true")
            ,key = {"order.failer","stock.unlock"}))
    public void unLock(String orderToken, Channel channel, Message message) throws IOException {
        //判读订单号是否为空
        if(StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //从redis缓存中查询锁库存信息
        String json = redisTemplate.opsForValue().get(STOCK_WARE+orderToken);
        if (StringUtils.isBlank(json)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        List<SkuLockVo> skuLockVos = JSON.parseArray(json, SkuLockVo.class);
        if (CollectionUtils.isEmpty(skuLockVos)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        //遍历解锁库存
        skuLockVos.forEach(skuLockVo -> {
            wareSkuMapper.unLock(skuLockVo.getSkuId(),skuLockVo.getWareId(),skuLockVo.getCount());
        });
        //删除redis中的缓存
        redisTemplate.delete(STOCK_WARE+orderToken);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(),
            exchange = @Exchange(value = "",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = ""))
    public void decLock(String outTradeNo,Channel channel,Message message) throws IOException {
        //判读订单号是否为空
        if(StringUtils.isBlank(outTradeNo)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //从redis缓存中查询锁库存信息
        String json = redisTemplate.opsForValue().get(STOCK_WARE+outTradeNo);
        if (StringUtils.isBlank(json)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        List<SkuLockVo> skuLockVos = JSON.parseArray(json, SkuLockVo.class);
        //扣减库存
        skuLockVos.forEach(skuLockVo -> {
            this.wareSkuMapper.minus(skuLockVo.getWareId(),skuLockVo.getCount());
        });

        //删除redis中的缓存
        redisTemplate.delete(STOCK_WARE+outTradeNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
